import axios from 'axios'
import Cookies from 'js-cookie'
import { common } from '@/assets/js/common.js'

axios.defaults.withCredentials = true;

export let user = {
    login: async (user_name, pwd) => {
        return axios.post('http://localhost/api/user/login', {
            'user_name': user_name,
            'pwd': pwd 
        })
        .then(res => {
            return res.data
        })
        .catch(error => error.response.data)
    },
    logout: async () => {
        if (common.isEmpty(Cookies.get('tk')) || common.isEmpty(Cookies.get('user'))){
            return false;
        }
        return axios.post('http://localhost/api/user/logout', {
            'user_name': Cookies.get('user')
        })
        .then(res => res.data)
        .catch(error => error.response.data)
    },
    
}


export let userCheck = {
    checkUserStatus: async () => {
        if (common.isEmpty(Cookies.get('tk')) || common.isEmpty(Cookies.get('user'))){
            return false;
        }
        return axios.post('http://localhost/api/user/checkUserStatus', {
            tk  : Cookies.get('tk'),
            user: Cookies.get('user')
        })
        .then(res => res.data)
        .catch(error => error.response.data)
    },
    apiTest: async (action, param) => {
        console.log(action, '->', param)
    
        let url = "http://localhost/api/test";
        switch(action){
            case "delCookie":
                url += "/delCookie";
                break;
            case "checkUserStatus":
                url += '/checkUserStatus'
                break
            case "other":
                break;
        }
    
        return axios.post(url, param)
        .then(res => res.data)
        .catch(error => error.response.data)
    
    }
}


